package com.example.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class Lock {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public boolean getLock(String lockID,long millisecond){
        Boolean b = stringRedisTemplate.opsForValue().setIfAbsent(lockID,"lock",millisecond, TimeUnit.MILLISECONDS);
        return b != null && b;
    }

    public void deleteLock(String lockId){
        stringRedisTemplate.delete(lockId);
    }
}
